{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 导入相关模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 470,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\"\"\"A very simple MNIST classifier.\n",
    "See extensive documentation at\n",
    "https://www.tensorflow.org/get_started/mnist/beginners\n",
    "\"\"\"\n",
    "from __future__ import absolute_import\n",
    "from __future__ import division\n",
    "from __future__ import print_function\n",
    "\n",
    "import argparse\n",
    "import sys\n",
    "\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "\n",
    "import tensorflow as tf\n",
    "\n",
    "FLAGS = None\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 导入Mnist数据\n",
    "我们在这里调用系统提供的Mnist数据函数为我们读入数据，如果没有下载的话则进行下载。\n",
    "\n",
    "<font color=#ff0000>**这里将data_dir改为适合你的运行环境的目录**</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 566,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting /input_data\\train-images-idx3-ubyte.gz\n",
      "Extracting /input_data\\train-labels-idx1-ubyte.gz\n",
      "Extracting /input_data\\t10k-images-idx3-ubyte.gz\n",
      "Extracting /input_data\\t10k-labels-idx1-ubyte.gz\n"
     ]
    }
   ],
   "source": [
    "# Import data\n",
    "data_dir = '/input_data'\n",
    "mnist = input_data.read_data_sets(data_dir, one_hot=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 573,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 定义添加隐层函数\n",
    "def add_layer(x, in_size, out_size, std,activation_function=None):\n",
    "   \n",
    "   W = tf.Variable(tf.truncated_normal([in_size, out_size],stddev=std))\n",
    "   b = tf.Variable(tf.truncated_normal([out_size],stddev=0.1))\n",
    "   logit = tf.matmul(x, W) + b\n",
    "   l2_loss = tf.nn.l2_loss(W)\n",
    "    \n",
    "   if activation_function is None:\n",
    "       outputs = logit \n",
    "   else:\n",
    "       outputs = activation_function(logit)\n",
    "   return outputs,l2_loss"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "定义我们的ground truth 占位符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 574,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Define loss and optimizer\n",
    "x = tf.placeholder(tf.float32, [None, 784]) #输入数据为n个784维向量\n",
    "y_ = tf.placeholder(tf.float32, [None, 10])#存放实际结果"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 模型部分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 579,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def swish(x):\n",
    "  return x*tf.nn.sigmoid(x)\n",
    "\n",
    "\n",
    "h1, loss1 = add_layer(x, 784, 100,0.05, activation_function=swish)\n",
    "#prediction , loss2= add_layer(h1, 100, 10, activation_function=tf.nn.softmax)\n",
    "prediction,loss2=add_layer(h1, 100, 10, 0.07,activation_function=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 580,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#交叉熵损失\n",
    "#cross_entropy=tf.reduce_mean(tf.reduce_sum(tf.square(y_ - prediction), reduction_indices=[1]))\n",
    "cross_entropy = tf.reduce_mean(\n",
    "    tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=prediction))\n",
    "#L2loss\n",
    "L2loss=loss1+loss2\n",
    "#totalloss\n",
    "totalloss=cross_entropy + 7e-5*L2loss\n",
    "\n",
    "#预测结果\n",
    "correct_prediction = tf.equal(tf.argmax(prediction, 1), tf.argmax(y_, 1))\n",
    "#准确率\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 581,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "lrate = 0.5\n",
    "train_step = tf.train.GradientDescentOptimizer(lrate).minimize(totalloss)\n",
    "\n",
    "\n",
    "sess = tf.Session()\n",
    "init_op = tf.global_variables_initializer()\n",
    "sess.run(init_op)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 582,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "总损失为 2.13711 ，准确率为 0.3211 l2损失为 77.5223 交叉熵损失为 2.13168\n",
      "总损失为 0.244079 ，准确率为 0.8899 l2损失为 104.096 交叉熵损失为 0.236792\n",
      "总损失为 0.188342 ，准确率为 0.9163 l2损失为 113.736 交叉熵损失为 0.180381\n",
      "总损失为 0.136078 ，准确率为 0.934 l2损失为 122.597 交叉熵损失为 0.127496\n",
      "总损失为 0.130724 ，准确率为 0.9423 l2损失为 131.066 交叉熵损失为 0.121549\n",
      "总损失为 0.102493 ，准确率为 0.9441 l2损失为 138.628 交叉熵损失为 0.0927886\n",
      "总损失为 0.125795 ，准确率为 0.952 l2损失为 145.082 交叉熵损失为 0.115639\n",
      "总损失为 0.139724 ，准确率为 0.9509 l2损失为 151.529 交叉熵损失为 0.129117\n",
      "总损失为 0.0916465 ，准确率为 0.9577 l2损失为 157.482 交叉熵损失为 0.0806228\n",
      "总损失为 0.140522 ，准确率为 0.9608 l2损失为 162.253 交叉熵损失为 0.129164\n",
      "总损失为 0.112983 ，准确率为 0.9611 l2损失为 167.481 交叉熵损失为 0.101259\n",
      "总损失为 0.0399302 ，准确率为 0.9645 l2损失为 172.776 交叉熵损失为 0.0278358\n",
      "总损失为 0.0566094 ，准确率为 0.9638 l2损失为 177.096 交叉熵损失为 0.0442126\n",
      "总损失为 0.0937608 ，准确率为 0.9686 l2损失为 181.621 交叉熵损失为 0.0810473\n",
      "总损失为 0.0480703 ，准确率为 0.9687 l2损失为 185.405 交叉熵损失为 0.0350919\n",
      "总损失为 0.0427209 ，准确率为 0.9675 l2损失为 189.26 交叉熵损失为 0.0294727\n",
      "总损失为 0.0387285 ，准确率为 0.9666 l2损失为 192.526 交叉熵损失为 0.0252517\n",
      "总损失为 0.0386975 ，准确率为 0.9701 l2损失为 195.998 交叉熵损失为 0.0249776\n",
      "总损失为 0.038062 ，准确率为 0.9715 l2损失为 199.968 交叉熵损失为 0.0240642\n",
      "总损失为 0.0336535 ，准确率为 0.9711 l2损失为 203.665 交叉熵损失为 0.0193969\n",
      "总损失为 0.0346624 ，准确率为 0.9726 l2损失为 206.961 交叉熵损失为 0.0201752\n",
      "总损失为 0.0588651 ，准确率为 0.9723 l2损失为 209.841 交叉熵损失为 0.0441763\n",
      "总损失为 0.0259577 ，准确率为 0.9729 l2损失为 212.201 交叉熵损失为 0.0111036\n",
      "总损失为 0.0297521 ，准确率为 0.9743 l2损失为 215.6 交叉熵损失为 0.0146601\n",
      "总损失为 0.0362215 ，准确率为 0.9694 l2损失为 218.65 交叉熵损失为 0.0209159\n",
      "总损失为 0.0484694 ，准确率为 0.9682 l2损失为 221.256 交叉熵损失为 0.0329814\n",
      "总损失为 0.0335163 ，准确率为 0.9764 l2损失为 223.947 交叉熵损失为 0.01784\n",
      "总损失为 0.0454522 ，准确率为 0.9744 l2损失为 226.679 交叉熵损失为 0.0295847\n",
      "总损失为 0.0897997 ，准确率为 0.9732 l2损失为 228.836 交叉熵损失为 0.0737811\n",
      "总损失为 0.0342805 ，准确率为 0.9761 l2损失为 231.942 交叉熵损失为 0.0180446\n"
     ]
    }
   ],
   "source": [
    "\n",
    "for i in range(3000):\n",
    "   batch_xs, batch_ys = mnist.train.next_batch(100) \n",
    "   if i<1800:\n",
    "      lrate = 1.2\n",
    "   elif i<3400:\n",
    "      lrate = 0.3\n",
    "   else:\n",
    "      lrate = 0.01\n",
    "    \n",
    "   \n",
    "   sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})\n",
    "   if i % 100 == 0:\n",
    "       # to see the step improvement\n",
    "       \n",
    "       \n",
    "       print(\"总损失为\",sess.run(totalloss, feed_dict={x: batch_xs, y_: batch_ys}),\n",
    "             \"，准确率为\",sess.run(accuracy, feed_dict={x: mnist.test.images,y_: mnist.test.labels}),\n",
    "             \"l2损失为\",sess.run(L2loss, feed_dict={x: batch_xs, y_: batch_ys}),\n",
    "             \"交叉熵损失为\",sess.run(cross_entropy, feed_dict={x: batch_xs, y_: batch_ys}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
